<?php
class News1_AdminItemController extends  Zendvn_Controller_Action{
	//thiet lap gia tri mac dinh cho phan trang
	private $_paning = array('rowCount'=> 2, 'pageRange'=> 3);
	
	public function init(){
		//load các thông số cơ bản 
		parent::init();		
		//load template 
		$this->_templateObj->setPath(TEMPLATE_PATH . '/admin/white');		
		$this->_templateObj->load();	
		
		/*====================================================
		 * Load Zendvn Translate for Controller 
		 * Thiết lập thông số cho language nằm trong ham parent::init() 
		 *====================================================*/
		$this->_langObj->setLangFile(array('contact.category.tmx','validate.tmx'));		
		Zend_Registry::set('Zend_Translate',$this->_langObj->generate());
		
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		/*====================================================
		 * xóa session
		 * $ss->unsetAll();
		 *====================================================*/
		if(empty($ss->col)){
			$ss->col = 'id';
			$ss->order = 'DESC';
		}
		$ss->setExpirationSeconds(600);
		$this->_arrParam['arrFilter'] = $ss->getIterator();
		$this->view->arrParam = $this->_arrParam;
		
		/*====================================================
		 * Phân trang 
		 *====================================================*/	
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_paning['rowCount'] = $this->_configObj->getSite('list_limit');		
		$this->_paning['pageRange'] = 3;				
		$this->_arrParam['panigator'] = $this->_paning;
		$this->view->arrParam = $this->_arrParam;
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrParam['module'],
												'fileConfig'=>'news1Category.ini'));
		$this->_config = $config->getconfig();
	}
	

	/*===============================================
	 * ACTION INDEX
	 *===============================================*/
	public function indexAction(){
		$this->view->Title = $this->view->translate('News1 Manager') . ' : '
							 .  $this->view->translate('Items') . ' : '
							 .  $this->view->translate('List');
		$this->view->headTitle($this->view->Title,true);
		
		$tableContact	  	= new Model_Table_News1Contact();
		$this->view->Item 	= $tableContact->listItem($this->_arrParam);
		//Tính  tổng các item đẻ  sửu dụng phân trang
		$totalItem = $tableContact->totalItem($this->_arrParam);
		
		$panigator = Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);		
		$this->view->panigator = $panigator;
		
		$tableCat = new Model_Table_news1Category();
		$this->view->slbCategory = $tableCat->itemInSelect($this->_arrParam);

	}
	
	
	/*===============================================
	 * ACTION STATUS
	 *===============================================*/	
	public function statusAction(){
		
		$tableContact = new Model_Table_News1Contact();
		
		if($this->_arrParam['type'] == 'multi'){
			$tableContact->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tableContact->statusItem($this->_arrParam);
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}	
	
	/*===============================================
	 * ACTION FILTER
	 *===============================================*/
	public function filterAction(){
			
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		
		//keyword
		if($this->_arrParam['type'] == 'search'){
			$ss->keyword = $this->_arrParam['keyword'];
			$ss->searchField = $this->_arrParam['search-field'];
		}
		
		if($this->_arrParam['type'] == 'clear'){
			$ss->keyword = '';
			$ss->searchField = '';
		}
		
		if($this->_arrParam['type'] == 'filter'){
			$ss->status = $this->_arrParam['status'];
			$ss->category = $this->_arrParam['category'];
		}
		
		if($this->_arrParam['type'] == 'sort'){
			$ss->col = $this->_arrParam['col'];
			$ss->order = $this->_arrParam['order'];
		}
		
		//chuyển hướng
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	/*===============================================
	 * ACTION INFO
	 *===============================================*/
	public function infoAction(){
		$this->view->Title = $this->view->translate('News1 Manager') . ' : '
							 .  $this->view->translate('Category') . ' : '
							 .  $this->view->translate('Info');
		$this->view->headTitle($this->view->Title,true);
		
		$tableContact = new Model_Table_News1Contact();
		$this->view->Item = $tableContact->getItem($this->_arrParam,array('task'=>'info'));

	 	$this->view->config = $this->_config['item'];
	}	
	
	/*===============================================
	 * ACTION EDIT
	 *===============================================*/
	public function editAction(){
		$this->view->Title = $this->view->translate('News1s Manager') . ' : '
							 .  $this->view->translate('Items') . ' : '
							 .  $this->view->translate('Edit');
		$this->view->headTitle($this->view->Title,true);
		
		$tableContact = new Model_Table_News1Contact();
		$this->view->Item = $tableContact->getItem($this->_arrParam);
		$this->view->slbParents = $tableContact->itemInSelect($this->_arrParam);
		if($this->_request->isPost()){		
			$validator = new News1_Form_ValidateNews1Category($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();
			}else{
				//xuất thông báo  cho người dùng
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();	
						
				$tableContact->saveItem($data,array('task'=>'admin-edit'));
				
				
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController 
									 . '/' . $this->_arrParam['action'] 
									 . '/id/' . $this->_arrParam['id']);
				}
			}		
		}
		$this->_helper->viewRenderer('form');
	}

	/*===============================================
	 * ACTION DELETE
	 *===============================================*/	
	public function deleteAction(){
				
		$tableContact = new Model_Table_News1Contact();
		
		if($this->_arrParam['type'] == 'multi'){
			$tableContact->deleteItem($this->_arrParam, array('task'=>'multi'));
		}else{
			$tableContact->deleteItem($this->_arrParam);
		}
		
		//xuất thông báo cho người dùng
		$this->_helper->flashMessenger->addMessage(array('success'=>'The delete was successful'));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}

	/*===============================================
	 * ACTION ADD
	 *===============================================*/		
	public function addAction(){
		$this->view->Title = $this->view->translate('News1 Manager') . ' : '
							 .  $this->view->translate('Category') . ' : '
							 .  $this->view->translate('Add');
		$this->view->headTitle($this->view->Title,true);
		
		$tableContact = new Model_Table_News1Contact();
		
		$this->view->slbParents = $tableContact->itemInSelect($this->_arrParam);
		if($this->_request->isPost()){	
			//kiểm tra dữ liệu		
			$validator = new News1_Form_ValidateNews1Category($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();				
			}else{
				//xuất thông báo cho người dùng
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();	
				$tableContact->saveItem($data,array('task'=>'admin-add'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController . '/' . $this->_arrParam['action']);
				}
			}
		}
		
		$this->_helper->viewRenderer('form');
	}

	
	/*===============================================
	 * ACTION REMOVE
	 *===============================================*/		
	public function removePicAction(){
		//1.Xoa hinh
		//2.Cap nhat database
		
		$config = $this->_config['item'];
		
		$upload = new Zendvn_File_Upload();
		$tableContact = new Model_Table_News1Contact();		

		$upload->remove($config['uploadDir'],$this->_arrParam['file']);
		$upload->remove($config['thumbDir'],$this->_arrParam['file']);			
		$tableContact->update(array('picture'=>''), 'id = ' . $this->_arrParam['id']);
					
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
	}
	
	
	
}